<?php

!defined('IN_APP') && exit('Access Denied');

class admin_usercontrol extends controlbase {


    function ondefault($msg='') {
//  @$page = max(1, intval($this->get['page']));
//  $pagesize=10;
//  $startindex = ($page - 1) * $pagesize;
//  $userlist = $this('user')->get_list($startindex,$pagesize);
//  $usernum=$this->db->selectCollection('user')->count();
//  $departstr=page($usernum, $pagesize, $page,"c=admin_user&a=default");
//  $msg && $message=$msg;
        include template('usersearch');
    }

    function onsearch() {
       @$page = max(1, intval($_REQUEST['page']));
        $pagesize=10;
        $startindex = ($page - 1) * $pagesize;
        //开始构造mongo查询条件$criteria
        $criteria=array();
        intval($_REQUEST['uid']) && $criteria['uid']=intval($_REQUEST['uid']);
        $_REQUEST['email'] && $criteria['email']=$_REQUEST['email'];
        $_REQUEST['username'] && $criteria['username']=$_REQUEST['username'];
        $_REQUEST['truename'] && $criteria['truename']=$_REQUEST['truename'];
        $_REQUEST['regtime1'] && $criteria['regtime']['$gt']=strtotime($_REQUEST['regtime1']);
        $_REQUEST['regtime2'] && $criteria['regtime']['$lt']=strtotime($_REQUEST['regtime2']);
        $_REQUEST['bday1'] && $criteria['bday']['$gt']=strtotime($_REQUEST['bday1']);
        $_REQUEST['bday2'] && $criteria['bday']['$lt']=strtotime($_REQUEST['bday2']);
        $_REQUEST['province'] && $criteria['province']=$_REQUEST['province'];
        $_REQUEST['city'] && ('请选择'!=$_REQUEST['city']) && $criteria['city']=$_REQUEST['city'];
	  $_REQUEST['district'] && ('请选择'!=$_REQUEST['district']) && $criteria['district']=$_REQUEST['district'];
        $_REQUEST['address'] && $criteria['address']=$_REQUEST['address'];
        //mongo条件查询
        $userlist = $this('user')->get_list($criteria ,$startindex , $pagesize);
        $usernum=$this->db->selectCollection('user')->find($criteria)->count();
        //分页url构造中，直接利用上述mongo查询条件$criteria。
        $criteria['regtime1']=strtotime($_REQUEST['regtime1']);
        $criteria['regtime2']=strtotime($_REQUEST['regtime2']);
        $criteria['bday1']=strtotime($_REQUEST['bday1']);
        $criteria['bday2']=strtotime($_REQUEST['bday2']);
        unset($criteria['regtime'],$criteria['bday']);
        $httpstr=http_build_query($criteria);
        $departstr=page($usernum, $pagesize, $page, 'c=admin_user&a=search&'.$httpstr);
        include template('userlist');
    }

    function onremove() {
        if(isset($this->post['uid'])) {
            $uids = $this->post['uid'];
            array_walk($uids ,function(&$item){$item = intval($item);});//需要把id转换为int类型
            $this('user')->remove($uids);
            $this->message('用户删除成功!','c=admin_user&a=search&'.$this->post['httpstr']);
        }
    }

    function onexport() {
		parse_str($this->post['httpstr'],$query);
		foreach($query as $k => $v){
			if(empty($v)) unset($query[$k]);
		}
		$fields=array('username','truename','email','format_regtime','format_bday','phone','address','idtype','idno','guardname','grelation','gidtype','gidno','urgencyname','urelation','uphone');
		$filepath='data/user'.$this->time.'.csv';
		$this('user')->export_csv($filepath , $query, $fields);
		header('location:'.$filepath);
    }
    
    function onadd() {
		if (isset($this->post['submit'])) {
	      		$username=$user['username']=$this->post['username'];
	      		$user['password']=$this->post['password'];
	      		$user['eid']=$this->post['eid']; //电子编号
	      		$email=$user['email']=$this->post['email'];
	      		$finduser=$this('user')->get_by_username($username);
	      		if($finduser) $this->message("用户名 $username 已经存在!", 'c=admin_user&a=add');
	      		$uid=$this('user')->add($user);
	      		$this->message('添加用户成功！', 'c=admin_user&a=add');
	        } 
	       include template('adduser');
    }
    
    function onedit() {
		$uid = intval($this->get['uid']);
		if (isset($this->post['submit'])) {
			$member = $this->post;
			unset($member['submit']);
			$this('user')->update($uid,$member);//保存个人信息
			$this->message("编辑用户成功", 'c=admin_user&a=edit&uid='.$uid);
        }
		$member = $this('user')->get_by_uid($uid);
	    include template('edituser');
    }

 

}
